package com.m3839.sdk.login.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.m3839.sdk.common.base.BaseDialogFragment;
import com.m3839.sdk.common.core.ApiError;
import com.m3839.sdk.common.core.Constant;
import com.m3839.sdk.common.core.ErrorConstant;
import com.m3839.sdk.common.event.SubjectBus;
import com.m3839.sdk.common.util.AppUtils;
import com.m3839.sdk.common.util.SpanUtils;
import com.m3839.sdk.common.util.StringUtils;
import com.m3839.sdk.common.widget.richtext.BetterLinkMovementMethod;
import com.m3839.sdk.common.widget.richtext.HtmlTagHandler;
import com.m3839.sdk.common.widget.richtext.LineSpaceExtraTextView;


/**
 * Description:维护公告的对话框
 *
 * @author 陈宝阳
 * @create 2020/8/10 14:46
 */
public class MaintenanceDialogFragment extends BaseDialogFragment {

  private static final String KEY_MAINTENANCE_CONTENT = "maintenance_content";

  private String content;

  private LineSpaceExtraTextView maintenanceContentTv;
  private Button negativeBtn;

  public static MaintenanceDialogFragment newInstance(String content) {
    MaintenanceDialogFragment fragment = new MaintenanceDialogFragment();
    Bundle data = new Bundle();
    data.putString(KEY_MAINTENANCE_CONTENT, content);
    fragment.setArguments(data);
    return fragment;
  }

  @Override
  protected void parseArgs() {
    content = getArguments().getString(KEY_MAINTENANCE_CONTENT);
  }

  @Override
  protected String getLayoutName() {
    return "hykb_maintenance_dialog_fragment";
  }

  @Override
  protected void initView(View view) {
    maintenanceContentTv = view.findViewById(getId("tv_maintenance_content"));
    negativeBtn = view.findViewById(getId("dlg_btn_negative"));
    //去掉点击链接的背景
    maintenanceContentTv.setHighlightColor(Color.parseColor("#00000000"));
  }

  @Override
  protected void setContent() {
    if (!TextUtils.isEmpty(content)) {
      BetterLinkMovementMethod betterLinkMovementMethod =
          BetterLinkMovementMethod.linkifyHtml(maintenanceContentTv)
              .setOnLinkClickListener(
                  new BetterLinkMovementMethod.OnLinkClickListener() {
                    @Override
                    public boolean onClick(TextView textView, String url) {

                      //增加温馨提示的链接点击统计
                      if (!StringUtils.isValidHttpUrl(url)) {
                        return true;
                      }

                      //打开url
                      AppUtils.openUrl(textView.getContext(), url);
                      return true;
                    }
                  });

      betterLinkMovementMethod.removeUrlHighlightColor(maintenanceContentTv);
      Spanned spanned = Html.fromHtml(content, null,
          new HtmlTagHandler(null));
      //超链接颜色
      int color = Color.parseColor("#23c268");
      maintenanceContentTv.setCustomText(SpanUtils.getHtmlUrlSpanStyle(spanned, color));
      maintenanceContentTv.setMovementMethod(betterLinkMovementMethod);
    }
  }

  @Override
  protected void initListener() {
    negativeBtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        // 退出游戏, 回调给游戏自己去做
        dismiss();
        SubjectBus.get().postValue(Constant.HYKB_EVENT_INIT_EXIT_GAME,
                new ApiError(ErrorConstant.HYKB_CODE_EXIT_GAME,
                        ErrorConstant.ERR_MSG_SDK_EXIT_GAME));
        AppUtils.killAllProcess(getActivity());
      }
    });
  }
}
